﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Commons.Web;
using Background.Core.DAL;
using Background.Core.Entity;
using Commons.Entity;
using System.Xml;
using System.Xml.Xsl;
using System.Text;
using System.IO;
namespace Background.Core.Controllers
{
    public class WebManageMenusController : BaseController
    {
        WebManageMenusRepository webManageMenusRepository;
        public WebManageMenusController()
        {
            webManageMenusRepository = new WebManageMenusRepository();
        }
        //
        // GET: /WebManageMenus/

        public ActionResult Index(int? page = 1)
        {
            var model = webManageMenusRepository.GetWebManageMenus(
                null,
                new PagingParam(page.Value, base.PageSize));
            return View(model);
        }

        //
        // GET: /WebManageMenus/Details/5

        public ActionResult Details(int id)
        {
            return View(webManageMenusRepository.GetEntity<WebManageMenus>(id));
        }

        //
        // GET: /WebManageMenus/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /WebManageMenus/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var entity = new WebManageMenus
            {
                UpdateDate = DateTime.Now,
                Status = 100,
                Operator = Current_UserName,
            };
            TryUpdateModel(entity);
            if (ModelState.IsValid != null)
            {
                try
                {
                    // TODO: Add insert logic here
                    webManageMenusRepository.Insert(entity);
                    return RedirectToAction("Index");
                }
                catch
                {
                    throw;
                }
            }
            return View();
        }

        //
        // GET: /WebManageMenus/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /WebManageMenus/Edit/5

        [HttpPost]
        public ActionResult Edit(WebManageMenus entity)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add update logic here
                    webManageMenusRepository.Update<WebManageMenus>(i => new WebManageMenus
                    {
                        MenuID = entity.MenuID,
                        MenuLevel = entity.MenuLevel,
                        MenuName = entity.MenuName,
                        Operator = Current_UserName,
                        ParentID = entity.ParentID,
                        SortNumber = entity.SortNumber,
                        Status = entity.Status,
                    });
                    return RedirectToAction("Index");
                }
                catch
                {
                    throw;
                }
            }
            return View();
        }

        //
        // GET: /WebManageMenus/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        public ActionResult PartialWebWebManageMenusTree()
        {
            XmlDocument xd = new XmlDocument();
            xd.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><root />");
            List<WebManageMenus> list = webManageMenusRepository.GetEntities<WebManageMenus>().ToList();
            var li = list.Where(item => item.ParentID == 0).OrderBy(item => item.SortNumber);
            foreach (WebManageMenus i in list.Where(item => item.ParentID == 0).OrderByDescending(item => item.SortNumber))
            {
                SerializeWebManageMenu(xd.DocumentElement, i, list);
            }


            XslCompiledTransform xct = new XslCompiledTransform(true);
            StringBuilder sb = new StringBuilder();
            XmlWriter xw = new XmlTextWriter(new StringWriter(sb));
            XsltArgumentList xal = new XsltArgumentList();
            xct.Load(System.AppDomain.CurrentDomain.BaseDirectory + "\\XSLT\\WebManageMenus.xslt");
            xct.Transform(xd, null, xw);
            xd.LoadXml(sb.ToString());
            if (Request.IsAjaxRequest())
            {
                return PartialView("PartialWebWebManageMenusTree", xd);
            }
            else
            {
                return View(xd);
            }
        }

        void SerializeWebManageMenu(XmlNode xn, WebManageMenus WebManageMenu, IList<WebManageMenus> linq)
        {
            XmlNode xnWebWebManageMenu = xn.OwnerDocument.CreateElement("WebManageMenus");
            XmlAttribute xa;

            xa = xn.OwnerDocument.CreateAttribute("WebManageMenusID");
            xa.Value = WebManageMenu.MenuID.ToString();
            xnWebWebManageMenu.Attributes.Append(xa);

            xa = xn.OwnerDocument.CreateAttribute("WebManageMenusName");
            xa.Value = WebManageMenu.MenuName;
            xnWebWebManageMenu.Attributes.Append(xa);

            xa = xn.OwnerDocument.CreateAttribute("About");
            xa.Value = WebManageMenu.About;
            xnWebWebManageMenu.Attributes.Append(xa);

            xa = xn.OwnerDocument.CreateAttribute("LinkURL");
            xa.Value = WebManageMenu.LinkUrl;
            xnWebWebManageMenu.Attributes.Append(xa);

            xa = xn.OwnerDocument.CreateAttribute("Status");
            xa.Value = WebManageMenu.Status.ToString();
            xnWebWebManageMenu.Attributes.Append(xa);

            foreach (WebManageMenus i in linq.Where(item => item.ParentID == WebManageMenu.MenuID).OrderBy(item => item.SortNumber))
            {
                SerializeWebManageMenu(xnWebWebManageMenu, i, linq);
            }
            xn.AppendChild(xnWebWebManageMenu);
        }

    }
}
